﻿package am.fp.toolbars
{ 
  import fl.controls.Button;                    
  import fl.controls.Label;
  import fl.controls.TextInput;                    
  import fl.events.ComponentEvent;

  import flash.events.TextEvent;
  import flash.events.FocusEvent;
  import flash.text.TextFormat;
  import flash.text.TextFieldAutoSize;

  import com.yahoo.astra.fl.containers.HBoxPane;
  
  import am.fp.Document;
  import am.fp.Toolbar;
  import am.fp.FPDispatcher;
  import am.fp.events.PageEvent;
  
  /**
   * Toolbar containing buttons and controls to display pages count, and view/change current page number.
   */
  public class Pages extends Toolbar
  { 
    private var m_tiCurrentPage: TextInput;
    private var m_lblPageCount: Label;
    private var m_btnPrev: Button;
    private var m_btnNext: Button;
    
    public function Pages()
    {
      var container: HBoxPane = addContainer(3, 2);
      
      var l: Label = new Label();
      l.text = "Page";
      l.autoSize = TextFieldAutoSize.LEFT;
      l.height = TEXTFIELDS_HEIGHT;
      container.addChild(l);
                                       
      var format: TextFormat = new TextFormat();
      format.bold = true;
      m_tiCurrentPage = new TextInput();
      m_tiCurrentPage.editable = true;
      m_tiCurrentPage.addEventListener(ComponentEvent.ENTER, onCurrentPageEnterKey, false, 0, true);
      m_tiCurrentPage.addEventListener(FocusEvent.FOCUS_OUT, onCurrentPageFocusOut, false, 0, true);
      m_tiCurrentPage.textField.autoSize = TextFieldAutoSize.LEFT;
      m_tiCurrentPage.restrict = "0-9";
      m_tiCurrentPage.width = 30;
      m_tiCurrentPage.height = TEXTFIELDS_HEIGHT;
      m_tiCurrentPage.maxChars = 4;
      m_tiCurrentPage.setStyle("textFormat", format);
      container.addChild(m_tiCurrentPage);
            
      m_lblPageCount = new Label();
      m_lblPageCount.text = " / ";
      m_lblPageCount.autoSize = TextFieldAutoSize.LEFT;
      m_lblPageCount.height = TEXTFIELDS_HEIGHT;
      container.addChild(m_lblPageCount);
      
      m_btnPrev = addButton("PrevIcon");
      m_btnNext = addButton("NextIcon");
      
      m_btnPrev.addEventListener(ComponentEvent.BUTTON_DOWN, onPrevPage, false, 0, true);
      m_btnNext.addEventListener(ComponentEvent.BUTTON_DOWN, onNextPage, false, 0, true);
      FPDispatcher.instance.addEventListener(PageEvent.PAGES_COUNT_UPDATED, onPagesCountUpdated, false, 0, true);
      FPDispatcher.instance.addEventListener(PageEvent.PAGE_NUM_UPDATED, onPageNumUpdated, false, 0, true);
    }
   
    private function onPrevPage(e: ComponentEvent): void
    {
      FPDispatcher.instance.dispatchEvent(new PageEvent(PageEvent.PAGE_PREV));
    }

    private function onNextPage(e: ComponentEvent): void
    {
      FPDispatcher.instance.dispatchEvent(new PageEvent(PageEvent.PAGE_NEXT));
    }

    private function onPagesCountUpdated(e: PageEvent): void
    { 
      m_lblPageCount.text = "/ " + e.pageNum;
      var bVisible: Boolean = Boolean(e.pageNum > 1);
      m_btnPrev.visible = bVisible;
      m_btnNext.visible = bVisible;
    }

    private function onPageNumUpdated(e: PageEvent): void
    {   
      m_tiCurrentPage.text = String(e.pageNum);
      m_btnPrev.enabled = (e.pageNum > 1);
      m_btnNext.enabled = (e.pageNum < Document.instance.pageCount);
    }
   
    private function onCurrentPageEnterKey(e: ComponentEvent): void
    {
      FPDispatcher.instance.dispatchEvent(new PageEvent(PageEvent.GOTO_PAGE, Math.min(Math.max(int(m_tiCurrentPage.text), 1), Document.instance.pageCount)));
      setFocus();
    }
    
    private function onCurrentPageFocusOut(e: FocusEvent): void
    {
      onCurrentPageEnterKey(null);
    }
    
    override protected function enableButtons(box: HBoxPane, bEnabled: Boolean): void
    {
      super.enableButtons(box, bEnabled);
      if (box == this && !bEnabled)
      {
        m_lblPageCount.text = "";
        m_tiCurrentPage.text = "";
      }
    }
  }
}

